﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using KL.UFrame.Api.Extensions;
using KL.UFrame.Core.Unity;
using KL.UFrame.Data.Business;
using KL.UFrame.Data.Models;

namespace KL.UFrame.Api.Controllers
{
    public class TrainningController : K9ControllerBase
    {
        private ITrainningBusiness _business = UnityBootstrapper.Current.GetService<ITrainningBusiness>();

        // GET: Trainning
        public async Task<ViewResult> Index()
        {
            var model = await _business.GetAllCategoriesAsync();
            return View(model.Data);
        }

        /// <summary>
        /// 培训列表
        /// </summary>
        /// <param name="id">分类Id</param>
        /// <returns></returns>
        public async Task<ViewResult> List(int id)
        {
            var category = await _business.GetCategoryAsync(id);
            if (category == null) return NotFound();
            ViewBag.Title = category.Name;
            var model = await _business.GetAllTrainningsAsync(id);
            return View(model.Data);
        }


        /// <summary>
        /// 报名
        /// </summary>
        /// <param name="id">培训id</param>
        /// <returns></returns>
        public async Task<ViewResult> Enroll(int id)
        {
            var trainning = await _business.GetTrainningAsync(id);
            if (trainning == null) return NotFound();

            var model = new EnrollModel
            {
                TrainningId = id,
                TrainningName = trainning.Title,
                UserLimit = trainning.UserLimit
            };
            return View(model);
        }

        [HttpPost]
        public async Task<ViewResult> Enroll(EnrollModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }
            model.CreateTime = DateTime.Now;
            
            var id= await _business.CreateEnrollAsync(model);

            var appConfigBusiness = UnityBootstrapper.Current.GetService<IAppConfigBusiness>();

            if (id > 0)
            {
                model.Id = id;
                ViewBag.Info =
                    await appConfigBusiness.GetContentByKeyAsync(AppConfigKeys.TrainningEnrollSuccess.ToString());
                return View("EnrollResult", model);
            }
            if (id == -1)
            {
                //超出名额
                var result = new ResultModel
                {
                    Title = "报名失败",
                    Content =
                        await appConfigBusiness.GetContentByKeyAsync(AppConfigKeys.TrainningEnrollOutOfLimit.ToString())
                };

                return View("Result", result);
            }
            ModelState.AddModelError("","报名失败，请稍后再试");
            return View(model);
        }

        public async Task<ViewResult> Search(string phone,string idCard,DateTime? start, DateTime? end)
        {
            if (start == null)
            {
                start = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
            }
            if (end == null)
            {
                end = start.Value.AddMonths(1);
            }

           // ViewBag.SearchModel = new {phone = phone, idCard = idCard, start = start.Value, end = end.Value};
            ViewBag.phone = phone;
            ViewBag.idCard = idCard;
            ViewBag.start = start.Value;
            ViewBag.end = end.Value;

            if (string.IsNullOrEmpty(phone) || string.IsNullOrEmpty(idCard)) return View();
            
            if (start > end)
            {
                return View();
            }
            if (start.Value.AddMonths(3) < end.Value)
            {
                ViewBag.Error = "查询时间跨度不能超过3个月";
                return View();
            }
            var model =await _business.GetEnrollsAsync(phone, idCard,start.Value.ToBeginning(), end.Value.ToEnd());
            return View(model);
        }
    }
}